package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerQueryDTO;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping
    public Result pageCustomers(CustomerQueryDTO customerQueryDTO) {
        log.info("查询客户列表");
        PageResult<Customer> customerPage = customerService.pageCustomers(customerQueryDTO);
        return Result.success(customerPage);
    }

    @PostMapping
    public Result addCustomer(@RequestBody Customer customer) {
        log.info("新增客户");
        boolean save = customerService.save(customer);
        return save ? Result.success() : Result.error("新增客户失败");
    }

    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        log.info("查询客户: {}", id);
        Customer customer = customerService.getById(id);
        return customer != null ? Result.success(customer) : Result.error("客户不存在");
    }

    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer) {
        log.info("更新客户: {}", customer);
        boolean update = customerService.updateById(customer);
        return update ? Result.success() : Result.error("更新客户失败");
    }

}
